package village.rainbow.slipin.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.util.Log;

public class DateUtil {

	public static String toPatternedDate(String pattern, String sourcePattern, String source) throws Exception{
		if(source.equals(""))
			return source;
		SimpleDateFormat sdf = new SimpleDateFormat(sourcePattern);
		Date d = sdf.parse(source);
		sdf.applyPattern(pattern);
		return sdf.format(d);
	}

	/**
	 * Calculate difference between two dates 
	 * unit - second
	 * @param fromDate - lower
	 * @param toDate   - higher
	 * @return
	 * @throws Exception
	 */
	@SuppressLint("SimpleDateFormat")
	public static int calculateTimeGab(String fromDate, String toDate) throws Exception{
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date1 = format.parse(fromDate);
		Date date2 = format.parse(toDate);
		long difference = date2.getTime() - date1.getTime();
		Log.i("village", fromDate + " / " + difference + " / " + (int)(difference/1000/60));
		return (int)(difference/1000);
	}
	
	/**
	 * Calculate difference between two dates 
	 * unit - minute
	 * @param fromDate - lower
	 * @param toDate   - higher
	 * @return
	 * @throws Exception
	 */
	@SuppressLint("SimpleDateFormat")
	public static int calculateTimeGabByMinute(String fromDate, String toDate) throws Exception{
		return calculateTimeGab(fromDate, toDate)/60;
	}
	
	/**
	 * Calculate difference between two dates
	 * unit - hour
	 * @param fromDate - lower
	 * @param toDate   - higher
	 * @return
	 * @throws Exception
	 */
	@SuppressLint("SimpleDateFormat")
	public static int calculateTimeGabByHour(String fromDate, String toDate) throws Exception{
		return calculateTimeGabByMinute(fromDate, toDate)/60;
	}
}
